package cz.muni.fi.pa165.machineryrental.converters;

import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import cz.muni.fi.pa165.machineryrental.entities.Machine;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Martin Klvana, 395893
 */
public class MachineDTOConverter {

    /**
     * Converts Machine entity to MachineDTO
     *
     * @param entity
     * @return MachineDTO which is set by Machine entity
     */
    public static MachineDTO toDTO(Machine entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        MachineDTO dto = new MachineDTO();
        MachineDTOConverter.applyToDTO(entity, dto);

        return dto;
    }

    /**
     * Converts MachineDTO to Machine entity
     *
     * @param dto
     * @return Machine entity which is set by MachineDTO
     */
    public static Machine toEntity(MachineDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        Machine entity = new Machine();
        entity.setId(dto.getId());
        entity.setBrand(dto.getBrand());
        entity.setCode(dto.getCode());
        entity.setModel(dto.getModel());
        entity.setYearOfProduction(dto.getYearOfProduction());

        return entity;
    }

    /**
     * Converts List of MachineDTO to Machine List
     *
     * @param dto list
     * @return Machine list
     */
    public static List<Machine> toEntityList(List<MachineDTO> list) {
        if (list == null || list.isEmpty()) {
            return new ArrayList<Machine>();
        }

        List<Machine> result = new ArrayList<Machine>();
        for (MachineDTO m : list) {
            result.add(MachineDTOConverter.toEntity(m));
        }

        return result;
    }

    /**
     * Converts machine entity to basic machine DTO
     *
     * @param entity Machine entity
     * @param dto Machine dto
     * @return Given modified machine dto.
     */
    public static MachineDTO applyToDTO(Machine entity, MachineDTO dto) {

        dto.setId(entity.getId());
        dto.setBrand(entity.getBrand());
        dto.setCode(entity.getCode());
        dto.setModel(entity.getModel());
        dto.setYearOfProduction(entity.getYearOfProduction());

        return dto;
    }

    /**
     * Converts list of machine entities to list of machine DTOs.
     *
     * @param machines Machines to convert.
     * @return List of DTOs according to given machiness.
     */
    public static List<MachineDTO> toDTOList(List<Machine> machines) {
        if (machines == null || machines.isEmpty()) {
            return new ArrayList<MachineDTO>();
        }

        List<MachineDTO> result = new ArrayList<MachineDTO>();
        for (Machine m : machines) {
            result.add(MachineDTOConverter.toDTO(m));
        }

        return result;
    }

    public static Machine toEntity(List<MachineDTO> findByModel) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
